﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ICeFramework.Win.UI.MDI
{
    public partial class MDIMainForm : Form
    {
        public event EventHandler LoadMDIChildren;
        public event EventHandler LoadMDIChildrenComplete;

        private MDIChildFormCollection _childForms;
        #region public MDIChildFormCollection ChildForms
        /// <summary>
        /// Get/Sets the ChildForms of the MDIMainForm
        /// </summary>
        /// <value></value>
        public MDIChildFormCollection ChildForms
        {
            get { return _childForms; }
            set { _childForms = value; }
        }
        #endregion

        private ToolStripMenuItem _windowMenu = new ToolStripMenuItem("Window");
        #region public ToolStripMenuItem WindowMenu
        /// <summary>
        /// Gets the WindowMenu of the MDIMainForm
        /// </summary>
        /// <value></value>
        public ToolStripMenuItem WindowMenu
        {
            get { return _windowMenu; }
        }
        #endregion

        #region public MDIMainForm()
        /// <summary>
        /// Initializes a new instance of the <b>MDIMainForm</b> class.
        /// </summary>
        public MDIMainForm()
        {
            InitializeComponent();
            _childForms = new MDIChildFormCollection();
            this.IsMdiContainer = true;
            this.FormClosing += new FormClosingEventHandler(MDIMainForm_FormClosing);
        }
        #endregion

        #region private void MDIMainForm_Load(object sender, EventArgs e)
        /// <summary>
        /// This method is called when the MDIMainForm's Load event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> of the event.</param>
        private void MDIMainForm_Load(object sender, EventArgs e)
        {
            if (LoadMDIChildren == null)
            {
                return;
            }

            LoadMDIChildren(this, EventArgs.Empty);

            int i = 0;
            int left = 5;
            int width = (panel1.Width / _childForms.Buttons.Count) - 10;
            _windowMenu.DropDownItems.Add(GetMenuItem("Cascade", "Cascade", new EventHandler(WindowMenuMenuItem_Click)));
            _windowMenu.DropDownItems.Add(GetMenuItem("TileHorizontally", "Tile Horizontally", new EventHandler(WindowMenuMenuItem_Click)));
            _windowMenu.DropDownItems.Add(GetMenuItem("TileVertically", "Tile Vertically", new EventHandler(WindowMenuMenuItem_Click)));
            _windowMenu.DropDownItems.Add(GetMenuItem("ArrangeIcons", "Arrange Icons", new EventHandler(WindowMenuMenuItem_Click)));
            //_windowMenu.DropDownItems.Add(new tools
            this.Resize += new EventHandler(MDIMainForm_Resize);
            foreach (Button btn in _childForms.Buttons)
            {
                this.panel1.Controls.Add(btn);
                btn.Top = 5;
                btn.Left = left;
                btn.Width = width;
                btn.Height = panel1.Height - 10;
                left += btn.Width + 5;
                btn.Click += new EventHandler(childFormActivator_Click);
                btn.Tag = i;
                i++;
            }
            if (LoadMDIChildrenComplete != null)
            {
                LoadMDIChildrenComplete(this, EventArgs.Empty);
            }

        }
        #endregion

        #region void MDIMainForm_Resize(object sender, EventArgs e)
        /// <summary>
        /// This method is called when the MDIMainForm's Resize event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> of the event.</param>
        void MDIMainForm_Resize(object sender, EventArgs e)
        {
            int left = 5;
            int width = (panel1.Width / _childForms.Buttons.Count) - 10;
            foreach (Button btn in _childForms.Buttons)
            {
                this.panel1.Controls.Add(btn);
                btn.Left = left;
                btn.Width = width;
                left += btn.Width + 5;
            }
        }
        #endregion

        #region void MDIMainForm_Disposed(object sender, EventArgs e)
        /// <summary>
        /// Releases the resources used by the <b>MDIMainForm</b>.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MDIMainForm_Disposed(object sender, EventArgs e)
        {
            sender = null;
        }
        #endregion

        #region void MDIMainForm_FormClosed(object sender, FormClosedEventArgs e)
        /// <summary>
        /// This method is called when the MDIMainForm's FormClosed event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="FormClosedEventArgs"/> of the event.</param>
        void MDIMainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            (sender as Form).Tag = null;
        }
        #endregion

        #region private ToolStripMenuItem GetMenuItem(string name, string text, EventHandler onClick)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="text"></param>
        /// <param name="onClick"></param>
        /// <returns></returns>
        private ToolStripMenuItem GetMenuItem(string name, string text, EventHandler onClick)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(text, null, onClick);
            item.Name = name;
            return item;
        }
        #endregion

        #region void MDIMainForm_FormClosing(object sender, FormClosingEventArgs e)
        /// <summary>
        /// This method is called when the MDIMainForm's FormClosing event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="FormClosingEventArgs"/> of the event.</param>
        void MDIMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (Form frm in _childForms.Forms)
            {
                if (frm != null)
                {
                    frm.Close();
                }
            }
            Environment.Exit(0);
        }
        #endregion

        #region void childFormActivator_Click(object sender, EventArgs e)
        /// <summary>
        /// This method is called when the childFormActivator's Click event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> of the event.</param>
        void childFormActivator_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            int idx = (int)btn.Tag;
            Type frmType = _childForms.FormTypes[idx] as Type;
            Form frm = _childForms.Forms[idx] as Form;
            
            if (frm == null || (frm != null && string.IsNullOrEmpty((string)frm.Tag)))
            {
                frm = (Form)Activator.CreateInstance(frmType);
                frm.MdiParent = this;
                frm.Tag = btn.Text;
                frm.FormClosed += new FormClosedEventHandler(MDIMainForm_FormClosed);
                _childForms.Forms[idx] = frm;
                frm.Show();
                frm.WindowState = FormWindowState.Maximized;
            }
            if (frm.WindowState == FormWindowState.Minimized)
            {
                frm.WindowState = FormWindowState.Maximized;
            }
            frm.Activate();
            frm.BringToFront();
        }
        #endregion

        #region void WindowMenuMenuItem_Click(object sender, EventArgs e)
        /// <summary>
        /// This method is called when the WindowMenuMenuItem's Click event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> of the event.</param>
        void WindowMenuMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            switch (item.Name)
            {
                case "Cascade":
                    this.LayoutMdi(MdiLayout.Cascade);
                    break;
                case "TileHorizontally":
                    this.LayoutMdi(MdiLayout.TileHorizontal);
                    break;
                case "TileVertically":
                    this.LayoutMdi(MdiLayout.TileVertical);
                    break;
                case "ArrangeIcons":
                    this.LayoutMdi(MdiLayout.ArrangeIcons);
                    break;
            }
        }
        #endregion

    }
}
